import axios from "axios";

const service = axios.create({
  baseURL: process.env.VUE_APP_BASEURL,
  // baseURL: '',
  timeout: 30000
});

// http request 拦截器
service.interceptors.request.use(
  config => {
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// http response 拦截器
service.interceptors.response.use(
  response => {
    const code = response.data.code;
    if (code === "500") {
      console.log(500);
    }
    return response;
  },
  error => {
    console.log(error);
    return Promise.reject(error);
  }
);

export const request = options => {
  return new Promise((resolve, reject) => {
    service(options)
      .then(response => {
        resolve(response.data);
      })
      .catch(error => {
        reject(error);
      });
  });
};

export const get = (url, params) => {
  return new Promise((resolve, reject) => {
    service.get(url, {
      params: params
    })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err);
      });
  });
};

export const post = (url, params) => {
  return new Promise((resolve, reject) => {
    service.post(url, params)
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err);
      });
  });
};